package mongodb;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.bson.Document;

import java.io.*;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/**
 * Created by lynn on 2016/11/14.
 */
public class HanSeg {
    public static void main(String[] args) {
        String tagsFile = "tags.txt";
        String hitsOutFile = "hit-tags.txt";
        String failedSegOutFile = "failed-tags.txt";
        if (args.length >= 1) {
            tagsFile = args[0];
        }
        if (args.length >= 2) {
            hitsOutFile = args[1];
        }
        if (args.length >= 3) {
            failedSegOutFile = args[2];
        }

        new HanSeg().f(tagsFile, hitsOutFile, failedSegOutFile);
    }


    void f(String tagsFile, String hitsOutFile, String failedSegOutFile) {
        String user = "nudtlab681";        // the user name
        String userdb = "baidu_info";    // the name of the database in which the user is defined
        char[] password = "nudtLabDataCenter2016".toCharArray();    // the password as a character array

        String dbname = userdb;
        ;
        List<MongoCredential> credentialList = new ArrayList<>();
        credentialList.add(MongoCredential.createScramSha1Credential(user, userdb, password));
        MongoClient mongoClient = new MongoClient(new ServerAddress("218.76.52.43", 3006), credentialList);
        MongoDatabase db = mongoClient.getDatabase(dbname);
        MongoCollection<Document> baiduArticles = db.getCollection("firstTry");

        Set<String> tags = Collections.EMPTY_SET;
        try (Reader tagsReader = new FileReader(tagsFile)) {
            tags = StreamSupport.stream(Spliterators.spliteratorUnknownSize(IOUtils.lineIterator(tagsReader), Spliterator.NONNULL | Spliterator.DISTINCT), true)
                    .map(String::trim).filter(line -> line.length() > 0).collect(Collectors.toSet());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Set<String> tagsHit = Collections.synchronizedSet(new HashSet<>(tags.size()));

//        Function<String, List<Term>> seger = SpeedTokenizer::segment;
//        final Set<String> finalWords = tags;
//        StreamSupport.stream(baiduArticles.find().spliterator(), true).forEach(doc -> {
//            try {
//                String text = (String) doc.get("AllText");
//                Set<String> segWords = seger.apply(text).stream().map(t -> t.word).collect(Collectors.toSet());
//                segWords.retainAll(finalWords);
//                tagsHit.addAll(segWords);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        });

        Set<String> failed = new HashSet<>(tags);
        failed.removeAll(tagsHit);

        try {
            FileUtils.writeLines(new File(hitsOutFile), "utf8", tagsHit);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (failed.size() > 0) {
            try {
                FileUtils.writeLines(new File(failedSegOutFile), "utf8", failed);
                System.out.println("分词不包含的tag：");
                System.out.println(failed);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
